// 引入api请求
import {
  requestLoginIn,//登录 获取到token 
  requestUserName,//获取用户名
  requestLoginOut
} from "@/api"
import tempMark from '@/util/tempMark'
const state = {
  userTempId: tempMark(),
  token: localStorage.getItem('user_Token') || '',//本地获取
  nickName: ""
}
const mutations = {
  // 设置token数据 并同时再本地存储保存一份
  setToken(state, payload) {
    state.token = payload
    localStorage.setItem('user_Token', payload)
  },
  // 设置用户名
  setNickName(state, payload) {
    state.nickName = payload
  },
  // 退出登录 清空数据
  EmptyList(state) {
    state.userTempId = ""
    state.token = ""
    state.nickName = ""
    localStorage.removeItem('user_Token')
  }
}
const actions = {
  // 登录获取token
  async getLoginInToken({ commit }, userInfo) {
    const res = await requestLoginIn(userInfo)
    commit('setToken', res.token)
  },
  // 获取用户名的请求
  async getNickName({ commit }) {
    const res = await requestUserName()
    commit('setNickName', res.nickName)
  },
  // 退出登录事件
  async loginOutCase({ commit }) {
    await requestLoginOut()
    commit('EmptyList')
  }
}
const getters = {
}
// 暴漏出去
export default {
  // 命名空间打开
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}